/*
 * ActionHistory.java
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster;

import java.util.Stack;
import org.fpdev.apps.rtemaster.actions.ACAction;

/**
 *
 * @author demory
 */
public class ActionHistory {

  private RouteMaster av_;
  private Stack<ACAction> undoStack_,  redoStack_;

  public ActionHistory(RouteMaster av) {
    av_ = av;
    undoStack_ = new Stack<ACAction>();
    redoStack_ = new Stack<ACAction>();
  }

  public void addAction(ACAction action) {
    undoStack_.add(action);
    redoStack_.clear();
    av_.getGUI().updateUndoRedo();
  }

  public boolean undoActionExists() {
    return !undoStack_.isEmpty();
  }

  public boolean redoActionExists() {
    return !redoStack_.isEmpty();
  }

  public String undoActionName() {
    if (undoStack_.isEmpty()) {
      return "";
    }
    return undoStack_.peek().getName();
  }

  public String redoActionName() {
    if (redoStack_.isEmpty()) {
      return "";
    }
    return redoStack_.peek().getName();
  }

  public void undoLast() {
    if (undoStack_.isEmpty()) {
      av_.msg("No actions to undo!");
      return;
    }
    ACAction action = undoStack_.peek();
    try {
      action.undoAction(av_);
    } catch (UnsupportedOperationException uoe) {
      av_.msg("Last action cannot be undone");
      return;
    }
    redoStack_.add(undoStack_.pop());
    av_.getGUI().updateUndoRedo();
  }

  public void redoLast() {
    if (redoStack_.isEmpty()) {
      av_.msg("No actions to redo!");
      return;
    }
    ACAction action = redoStack_.peek();
    try {
      action.doAction(av_);
    } catch (UnsupportedOperationException uoe) {
      av_.msg("Last action cannot be redone");
      return;
    }
    undoStack_.add(redoStack_.pop());
    av_.getGUI().updateUndoRedo();
  }
}
